package com.wmlive.hhvideo.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import cn.wmlive.hhvideo.R;

/**
 * 自定义对话框
 *
 * @author feijoy
 */
public class CustomDialog extends Dialog {
    private Context mContext; // 上下文对象
    private String mTitle; // 对话框标题
    private String mConfirmBtnText; // 按钮名称“确定”
    private String mCancelBtnText; // 按钮名称“取消”
    private OnClickListener mConfirmBtnClickListener;
    private OnClickListener mCancelBtnClickListener;
    private TextView title, confirm_btn, cancel_btn;

    public CustomDialog(Context context) {
        super(context, R.style.BaseDialogTheme);
        mContext = context;
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
    }

    public void setTitle(int title) {
        mTitle = (String) mContext.getText(title);
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public void setPositiveButton(int confirm_btnText,
                                  OnClickListener listener) {
        mConfirmBtnText = (String) mContext.getText(confirm_btnText);
        mConfirmBtnClickListener = listener;
    }


    public void setPositiveButton(String confirm_btnText,
                                  OnClickListener listener) {
        mConfirmBtnText = confirm_btnText;
        mConfirmBtnClickListener = listener;
    }

    public void setNegativeButton(int cancel_btnText,
                                  OnClickListener listener) {
        mCancelBtnText = (String) mContext.getText(cancel_btnText);
        mCancelBtnClickListener = listener;
    }

    public void setNegativeButton(String cancel_btnText,
                                  OnClickListener listener) {
        mCancelBtnText = cancel_btnText;
        mCancelBtnClickListener = listener;
    }

    @Override
    public void show() {
        super.show();
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.dialog_custom, null);
        addContentView(layout, new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        title = (TextView) layout.findViewById(R.id.title);
//        title.getPaint().setFakeBoldText(true);
        if (TextUtils.isEmpty(mTitle)) {
            title.setVisibility(View.GONE);
        } else {
            title.setText(mTitle);
        }

        confirm_btn = ((TextView) layout.findViewById(R.id.confirm_btn));
        cancel_btn = ((TextView) layout.findViewById(R.id.cancel_btn));

        if (TextUtils.isEmpty(mConfirmBtnText)) {
            confirm_btn.setVisibility(View.GONE);
        } else {
            confirm_btn.setText(mConfirmBtnText);
            if (mConfirmBtnClickListener != null) {
                confirm_btn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        mConfirmBtnClickListener.onClick(CustomDialog.this,
                                DialogInterface.BUTTON_POSITIVE);
                    }
                });
            }
        }

        if (TextUtils.isEmpty(mCancelBtnText)) {
            cancel_btn.setVisibility(View.GONE);
        } else {
            cancel_btn.setText(mCancelBtnText);
            if (mCancelBtnClickListener != null) {
                cancel_btn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        mCancelBtnClickListener.onClick(CustomDialog.this,
                                DialogInterface.BUTTON_NEGATIVE);
                    }
                });
            }
        }
        setContentView(layout);
    }
}